package com.example.test_witns.ui.my_qrcode;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.test_witns.R;
import com.example.test_witns.databinding.ActivityMyQrcodeBinding;
import com.example.test_witns.utils.QrCodeUtils;

import java.util.Objects;

import de.hdodenhof.circleimageview.CircleImageView;

public class MyQRCodeActivity extends AppCompatActivity {
    private ActivityMyQrcodeBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMyQrcodeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Objects.requireNonNull(getSupportActionBar()).hide();
        Window window = getWindow();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        window.setStatusBarColor(Color.parseColor("#EFFAFF"));

        View qr_code_back = findViewById(R.id.qr_code_back);
        qr_code_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        View qr_code_start_scan = findViewById(R.id.qr_code_start_scan);
        qr_code_start_scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), VerticalScannerActivity.class);
                startActivity(intent);
            }
        });

        Intent intent = getIntent();
        byte[] argumentBytes = intent.getByteArrayExtra("argumentBytes");
        String argumentString1 = intent.getStringExtra("argumentString1");
        String argumentString2 = intent.getStringExtra("argumentString2");
        String argumentString3 = intent.getStringExtra("argumentString3");
        String argumentString4 = intent.getStringExtra("argumentString4");
        CircleImageView qr_code_my_avatar = findViewById(R.id.qr_code_my_avatar);
        if (argumentBytes != null) {
            qr_code_my_avatar.setImageBitmap(BitmapFactory.decodeByteArray(argumentBytes, 0, argumentBytes.length));
        }else {
            Glide.with(getApplicationContext()).load(argumentString4).into(qr_code_my_avatar);
        }
        TextView qr_code_my_nickname = findViewById(R.id.qr_code_my_nickname);
        qr_code_my_nickname.setText(argumentString1);
        TextView qr_code_my_email = findViewById(R.id.qr_code_my_email);
        qr_code_my_email.setText(argumentString2);
        ImageView imageView = findViewById(R.id.qr_code);
        QrCodeUtils.generateTransparentQrCode(argumentString1 + "," + argumentString2 + "," + argumentString3 + "," + argumentString4, imageView);
        TextView qr_code_my_uuid = findViewById(R.id.qr_code_my_uuid);
        qr_code_my_uuid.setText("uuid: " + argumentString3);
    }
}
